import com.jiazhong.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

public class MybatisDao {

    //日志
   private static SqlSession sqlSession;
    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("Mybatis_comfig.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //从配置文件中获取一个工厂对象
//        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //通过工厂获取sqlsession对象
         sqlSession = factory.openSession();

    }
    public static void rizhi() throws IOException {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }
//    public SqlSession sqlSession() throws IOException {
//        InputStream resourceAsStream = Resources.getResourceAsStream("Mybatis_comfig.xml");
//
//        //从配置文件中获取一个工厂对象
////        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//
//        //通过工厂获取sqlsession对象
//        SqlSession sqlSession = factory.openSession();
//        return sqlSession;
//    }
    public static void updatestu(){
        //sqlsession对象执行mapper映射中的SQL语句[更新]
        int updated = sqlSession.update("EmpMapper.updatestudent");
        sqlSession.commit();
        System.out.println("updated = " + updated);

    }

    public static void insertstu(){
        //sqlsession对象执行mapper映射中的SQL语句[添加]
        int insert = sqlSession.insert("EmpMapper.insertstudent");
        sqlSession.commit();
        System.out.println("insert = " + insert);
    }

    public static void delete(){
        //sqlsession对象执行mapper映射中的SQL语句[删除]
        int delete = sqlSession.delete("EmpMapper.deletestudent");
        sqlSession.commit();
        System.out.println("delete = " + delete);
    }

    public static void selectList(){
        //sqlsession对象执行mapper映射中的SQL语句[查]
        List<Student> students = sqlSession.selectList("EmpMapper.findAll");
        System.out.println("15");
        for (Student stu:students){
            System.out.println("stu = " + stu);
        }
    }

    public static void insertsomeone(){
        HashMap map = new HashMap();
        map.put("sname", "王雪");//哈希map添加元素语句
        map.put("ssex", "女");
        map.put("sage", 29);
        map.put("sdepartment","永劫无间");
        int update = sqlSession.update("EmpMapper.insertdata",map);//传参
        sqlSession.commit();
        System.out.println("更新的数据 " + update);
    }

    public static void updatesomeone(){
        //sqlsession对象执行mapper映射中的SQL语句[更新更新对象]
        Student student = new Student();
        student.setSname("博人");
        student.setSsex("变男了");
        student.setSage(23);
        //通过工厂获取sqlsession对象
        int update = sqlSession.update("EmpMapper.updatedata",student);
        sqlSession.commit();
        System.out.println("更新的记录行数是 = " + update);
    }
    //sqlsession对象执行mapper映射中的SQL语句[动态查询字段名]
    public static void findfield(){

        HashMap stuhashMap = new HashMap();
        stuhashMap.put("cols","sname,sage");
        List<Student> studentList = sqlSession.selectList("EmpMapper.findfield",stuhashMap);
        System.out.println("studentList = " + studentList);
    }
    public  static void findfuzzy(){
        HashMap student = new HashMap();
        student.put("sname","人");
        List<Student> objects = sqlSession.selectList("EmpMapper.findfuzzy", student);
        System.out.println("objects = " + objects);

    }





    public static void cs() throws IOException {

        //日志
        rizhi();
//        Properties properties = new Properties();
//        FileInputStream fileInputStream = new FileInputStream("src/main/resources/log4j.properties");
//        properties.load(fileInputStream);
//        PropertyConfigurator.configure(properties);


        //获取连接配置文件
//        testbatis testbatis = new testbatis();
//        SqlSession sqlSession = testbatis.sqlSession();
//        InputStream resourceAsStream = Resources.getResourceAsStream("Mybatis_comfig.xml");
//
//        //从配置文件中获取一个工厂对象
////        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //通过工厂获取sqlsession对象
//        SqlSession sqlSession = factory.openSession();


        //sqlsession对象执行mapper映射中的SQL语句[更新]
//        int updated = sqlSession.update("EmpMapper.updatestudent");
//        sqlSession.commit();
//        System.out.println("updated = " + updated);

        //sqlsession对象执行mapper映射中的SQL语句[添加]
//        int insert = sqlSession.insert("EmpMapper.insertstudent");
//        sqlSession.commit();
//        System.out.println("insert = " + insert);
        //sqlsession对象执行mapper映射中的SQL语句[删除]
//        int delete = sqlSession.delete("EmpMapper.deletestudent");
//        sqlSession.commit();
//        System.out.println("delete = " + delete);
        //sqlsession对象执行mapper映射中的SQL语句[查]
//        List<Student> students = sqlSession.selectList("EmpMapper.findAll");
//        System.out.println("15");
//        for (Student stu:students){
//            System.out.println("stu = " + stu);
//        }
        //sqlsession对象执行mapper映射中的SQL语句[添加添加对象]
        //准备数据
//        HashMap map = new HashMap();
//        map.put("sname", "王雪");//哈希map添加元素语句
//        map.put("ssex", "女");
//        map.put("sage", 29);
//        map.put("sdepartment","永劫无间");
//        int update = sqlSession.update("EmpMapper.insertdata",map);//传参
//        sqlSession.commit();
//        System.out.println("更新的数据 " + update);
        //sqlsession对象执行mapper映射中的SQL语句[更新更新对象]
//        Student student = new Student();
//        student.setSname("博人");
//        student.setSsex("变男了");
//        student.setSage(23);
//        //通过工厂获取sqlsession对象
//
//         update = sqlSession.update("EmpMapper.updatedata",student);
//        sqlSession.commit();
//        System.out.println("更新的记录行数是 = " + update);
    }


    }

